package com.hsy.crm.web.web.basics;

import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.Response;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.Vendor;
import com.hsy.crm.web.domain.vo.vendor.VendorPageVo;
import com.hsy.crm.web.service.base.CacheService;
import com.hsy.crm.web.service.impl.VendorService;
import com.hsy.crm.web.web.BaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/vendor")
public class VendorAction extends BaseAction {

    @Autowired
    VendorService vendorService;

    @RequestMapping("/insert")
    public String insert(Vendor vendor) {
        Unified<Integer> unified = vendorService.insertVendor(vendor);
        if( unified.getResult() == App.Result.SUCCESS ){
            CacheService.putVendorId(vendor.getCustomerId(),vendor.getId());
        }
        return Response.body(unified);
    }

    @RequestMapping("/update")
    public String update(Vendor vendor) {
        return Response.body(vendorService.updateVendor(vendor));
    }

    @RequestMapping("/delete")
    public String delete(Vendor vendor) {
        Unified<Integer> unified = vendorService.deleteVendor(vendor);
        if( unified.getResult() == App.Result.SUCCESS ){
            CacheService.removeVendorId(vendor.getCustomerId(),vendor.getId());
        }
        return Response.body(unified);
    }

    @RequestMapping("/get")
    public String get(Vendor vendor) {
        return Response.body(vendorService.getVendor(vendor));
    }

    @RequestMapping("/page")
    public String page(VendorPageVo vendorPageVo) {
        return Response.body(vendorService.page(vendorPageVo));
    }

}
